/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-05-29
 * Time: 14:35
 */
public class BinarySearchTree {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public TreeNode root;


    /**
     * 查找
     *
     * @return
     */
    public TreeNode find(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val < key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    //添加
    public void add(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        TreeNode newNode = new TreeNode(key);
        if (cur == null) {
            root = newNode;
            return;
        }
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                return;
            }
        }
        if (parent.val > key) {
            parent.left = newNode;
        } else {
            parent.right = newNode;
        }
    }

    //删除
    public void delete(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (key > cur.val) {
                parent = cur;
                cur = cur.right;
            } else if (key < cur.val) {
                parent = cur;
                cur = cur.left;
            } else {
                //删除结点
                remove(parent, cur);
                return;
            }
        }
    }

    // 分情况讨论
    private void remove(TreeNode parent, TreeNode del) {
        //情况1:del.left==null
        if (del.left == null) {
            //情况1.1 del是root
            if (del == root) {
                root = del.right;
            } else {
                if (del == parent.left) {
                    //情况1.2 del不是root,del是parent.left
                    parent.left = del.right;
                } else if (del == parent.right) {
                    //情况1.3 del不是root,del是parent.right
                    parent.right = del.right;
                }
            }
        } else if (del.right == null) {
            //情况2:del.right==null
            if (del == root) {
                //情况2.1 del是root
                root = del.left;
            } else {
                if (del == parent.left) {
                    //情况2.2 del是parent.left
                    parent.left = del.left;
                } else if (del == parent.right) {
                    //情况2.3 del是parent.right
                    parent.right = del.left;
                }
            }
        } else {
            //情况3 del.left和del.right都不为空
            //将del右树的值最小的结点或者左树的值最大的结点和del替换
            //右边找
            TreeNode target = del.right;
            TreeNode targetP = del;
            while (target.left != null) {
                targetP = target;
                target = target.left;
            }
            //删除之前先交换
            del.val = target.val;
            //分情况删除
            if (target == targetP.right) {
                targetP.right = target.right;
            }
            if (target == targetP.left) {
                targetP.left = target.right;
            }
        }
    }
}
